package com.jstyle.jclifexd.utils;

import android.content.Context;
import android.util.Log;

import java.util.HashMap;

import cn.sharesdk.facebook.Facebook;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.Platform.ShareParams;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;

import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;

import cn.sharesdk.wechat.friends.Wechat;


public class ShareTools implements PlatformActionListener {
	private Context context;
	private static final String TAG = "ShareTools";
	private String tempFilePath;
	private ShareListener listener;
	//private SharePrefenceUtils spUtils;
	private String qq = "QQ";

	public ShareTools(Context context,String tempFilePath) {
//		ShareSDK.initSDK();
		this.context = context;
		this.tempFilePath=tempFilePath;
		//spUtils = new SharePrefenceUtils(context, "jstyle_userinfo");
		// String baseDir = Environment.getExternalStorageDirectory()
		// .getAbsolutePath() + "/" + context.getPackageName();
		// String sharePath = baseDir + "/images/share/";
		// tempFilePath = sharePath + ImageUtils.md5Encoder("tempFile");
	}

//	/**
//	 * 分享到qq空间
//	 */
//	public void QzoneShare() {
//		Platform qzone = ShareSDK.getPlatform(QZone.NAME);
//		ShareParams sp = new ShareParams();
//		sp.setShareType(Platform.SHARE_IMAGE);
////		sp.setTitle("J-STYLE LIFE");
////		sp.setTitleUrl("http://jstyle.jointcorp.com"); // 标题的超链接
////		sp.setText(shareText);
//		sp.setImagePath(ImageUtils.shotFilePath);
//		qzone.setPlatformActionListener(this);
//		qzone.share(sp);
//	}

	public void FacebookShare() {
    	Platform facebook = ShareSDK.getPlatform(Facebook.NAME);
		ShareParams sp = new ShareParams();
		sp.setShareType(Platform.SHARE_IMAGE);
		String path = ImageUtils.shotFilePath;
		sp.setImagePath(path);
		sp.setText("J-STYLE HEARTRATE");
		facebook.setPlatformActionListener(this);
		facebook.share(sp);
	}

//	public void TwitterShare() {
//		Platform qzone = ShareSDK.getPlatform(Twitter.NAME);
//		ShareParams sp = new ShareParams();
//		sp.setShareType(Platform.SHARE_IMAGE);
//		String path = ImageUtils.shotFilePath;
//		sp.setImagePath(path);
//		qzone.setPlatformActionListener(this);
//		qzone.share(sp);
//	}

	/**
	 * 分享到微信
	 */
	public void WechartShare(String shareText) {
		Platform weixin = ShareSDK.getPlatform(Wechat.NAME);
		ShareParams oks = new ShareParams();
		oks.setShareType(Platform.SHARE_IMAGE);
		oks.setText(shareText);
		oks.setTitle("分享");
		// oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");//网络图片
		String path = ImageUtils.shootPath+tempFilePath;
		oks.setImagePath(path);
		// oks.setUrl("http://jstyle.jointcorp.com");
		// weixin.setPlatformActionListener(this);
		weixin.share(oks);
	}

//	public void WechartCicleShare(String shareText) {
//		Platform weixin = ShareSDK.getPlatform(WechatMoments.NAME);
//
//		ShareParams oks = new ShareParams();
//		oks.setShareType(Platform.SHARE_IMAGE); // 分享到网页的方式打开连接
//		oks.setText(shareText);
//		oks.setTitle("J-STYLE LIFE");
//		// oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");//网络图片
//		String path = ImageUtils.shotFilePath;
//		oks.setImagePath(path);
//		// oks.setUrl("http://jstyle.jointcorp.com");
//		// weixin.setPlatformActionListener(this);
//		weixin.share(oks);
//	}

	/*
	 * 分享到新浪微博
	 */
	public void SinaWeiboShare(String shareText) {
		Platform weibo = ShareSDK.getPlatform(SinaWeibo.NAME);
		ShareParams sp = new ShareParams();
		sp.setText(shareText);
		String path = ImageUtils.shotFilePath;
		sp.setImagePath(path);
		sp.setContentType(SinaWeibo.SHARE_IMAGE);
		weibo.setPlatformActionListener(this);
		weibo.share(sp);
	}
	public void Share(String name) {
		Platform platform = ShareSDK.getPlatform(name);
		ShareParams sp = new ShareParams();
		if(!name.equals(QQ.NAME)){//qq不能添加文字
			sp.setText("睡眠数据");
		}
		String path = ImageUtils.shootPath+tempFilePath;
		sp.setImagePath(path);
		sp.setShareType(Platform.SHARE_IMAGE);
		platform.setPlatformActionListener(this);
		platform.share(sp);
	}

//	public void ShareLinkedin(String url) {
//		Platform platform = ShareSDK.getPlatform(LinkedIn.NAME);
//		ShareParams sp = new ShareParams();
//		sp.setComment("睡眠数据");
//		sp.setImageUrl(url);
//		sp.setTitleUrl(url);
//		sp.setSiteUrl(url);
//		sp.setTitle("睡眠数据");
//		sp.setShareType(Platform.SHARE_IMAGE);
//		platform.setPlatformActionListener(this);
//		platform.share(sp);
//	}
	public void twitterLogin() {
//		Platform twitter = ShareSDK.getPlatform(Twitter.NAME);
//		twitter.removeAccount();
//		twitter.setPlatformActionListener(this);
//		twitter.SSOSetting(false);
//		twitter.showUser(null);
	}
	public void facebookLogin() {
//		Platform facebook = ShareSDK.getPlatform(Facebook.NAME);
//		facebook.removeAccount();
//		facebook.setPlatformActionListener(this);
//		facebook.SSOSetting(false);
//		facebook.showUser(null);
	}
	public void qqLogin() {
		Platform qq = ShareSDK.getPlatform(QQ.NAME);
		qq.removeAccount(true);
		qq.setPlatformActionListener(this);
		qq.SSOSetting(false);
		qq.showUser(null);
	}

	public void sinaLogin() {
		Platform weibo = ShareSDK.getPlatform(SinaWeibo.NAME);
		weibo.removeAccount(true);
		weibo.SSOSetting(false);
		weibo.setPlatformActionListener(this);
		weibo.showUser(null);
	}

	public void wechatLogin() {
		Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
		wechat.removeAccount(true);
		wechat.SSOSetting(false);
		wechat.setPlatformActionListener(this);
		wechat.showUser(null);
	}

	@Override
	public void onCancel(Platform arg0, int arg1) {
		// TODO Auto-generated method stub
		Log.i(TAG, "onCancel: ");
		if (listener != null) {
			this.listener.shareCancel();
		}
	}

	@Override
	public void onComplete(Platform arg0, int arg1, HashMap<String, Object> res) {
		String name, profile_image_url;
		PlatformDb platDB = arg0.getDb();
		//spUtils.setSpString(DeviceConstant.userId,platDB.getUserId());
		String userId=platDB.getUserId();
		String platName=platDB.getPlatformNname();
		Log.i(TAG, "onComplete: "+platDB.getUserId());
		if (platDB.getPlatformNname().equals("QQ")) {
			profile_image_url = res.get("figureurl_qq_2").toString();
		} else {
			profile_image_url = platDB.getUserIcon();
		}
		name = platDB.getUserName();
		if (listener != null) {
			this.listener.ShareSuccess(name, profile_image_url,platName,userId);
		}

	}

	@Override
	public void onError(Platform arg0, int arg1, Throwable arg2) {
		Log.i(TAG, "onError: "+arg2.getMessage());
		if (listener != null) {
			this.listener.ShareFailed();
		}
	}

	public interface ShareListener {
		public void ShareSuccess(String userName, String headUrl, String platName, String userId);

		public void ShareFailed();

		public void shareCancel();
	}

	public void setShareListener(ShareListener listener) {
		this.listener = listener;
	}
}
